package com.zyx.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyx.mall.entity.Address;
import com.zyx.mall.entity.queryObject.AddressQuery;
import com.zyx.mall.service.AddressService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  收货地址
 * </p>
 *
 * @author zyx
 * @since 2023-01-06
 */
@RestController
@RequestMapping("/api/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    //添加收货地址
    @PostMapping("/add")
    public Boolean addAddress(@RequestBody Address address){
        if (address.getIsDefault()==0){
            //把该用户的原来的默认地址设为非默认
            QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",address.getUserId())
                    .eq("is_default",0);
            Address one = addressService.getOne(queryWrapper);
            if (one!=null){
                one.setIsDefault(1);
                addressService.updateById(one);
            }
        }
        addressService.save(address);
        return true;
    }

    //删除收货地址
    @DeleteMapping("/del")
    public Boolean delAddress(String id){
        return addressService.removeById(id);
    }



    //更新收货地址
    @PutMapping("/update")
    public Boolean updateAddress(@RequestBody Address address){
        Address address1 = addressService.getById(address.getId());
        if (address.getIsDefault()==0){
            //把该用户的原来的默认地址设为非默认
            QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",address1.getUserId())
                    .eq("is_default",0);
            Address one = addressService.getOne(queryWrapper);
            if (one!=null){
                one.setIsDefault(1);
                addressService.updateById(one);
            }
        }

        return addressService.updateById(address);
    }
    //更新默认收货地址
    @PutMapping("/updateDefault")
    public Boolean updateDefault(@RequestBody AddressQuery addressQuery){
        String userId = addressQuery.getUserId();
        String id = addressQuery.getId();
        //把该用户的原来的默认地址设为非默认
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId)
                .eq("is_default",0);
        Address one = addressService.getOne(queryWrapper);
        if (one!=null){
            one.setIsDefault(1);
            addressService.updateById(one);
        }

        //把该地址设为默认地址
        Address address = addressService.getById(id);
        address.setIsDefault(0);
        addressService.updateById(address);
        return true;

    }

    //查询收货地址
    @GetMapping("/list")
    public List<Address> getAddress(AddressQuery addressQuery){
        String userId = addressQuery.getUserId();
        String id = addressQuery.getId();
        String isDefault = addressQuery.getIsDefault();

        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(id),"id",id)
                .eq(StringUtils.isNotEmpty(userId),"user_id",userId)
                .eq(StringUtils.isNotEmpty(isDefault),"is_default",isDefault);
        return addressService.list(queryWrapper);
    }


}

